<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
    >
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column prop="specsname" label="属性名称" width="120"></el-table-column>
      <el-table-column
        prop="attrs"
        label="属性值"
        width="200"
      >
        <template slot-scope="scope">
          <el-tag
            v-for="(item,index) in scope.row.attrs"
            :key="index" 
            type="success"
          >{{item}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.uid)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- size-change：切换每页显示多少条 -->
    <!-- current-change：切换页码 -->
    <!-- current-page：当前页码 -->
    <!-- page-size：每页显示几条 -->
    <!-- total：总条数 -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="size"
      layout="prev, pager, next, jumper, total"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getSpecsList } from "../../request";
export default {
  data() {
    return {
      tableData: [],
      // 默认显示第一页
      currentPage: 1,
      // 每页显示几条
      size: 2,
      // 总条数
      total: 1
    };
  },
  created() {
    // 通过$bus的$on方法，开启监听
    this.$bus.$on("update", () => {
      this.getTotalNum();
    });
    // 获取总条数
    this.getTotalNum();
  },
  methods: {
    // 需要先获取总数量，才能就计算分成多少页，才能查看某一页
    async getTotalNum() {
      // let res = await getCount();
      // this.total = res.list[0].total;
      // 获取总数量之后，调用分页的请求
      this.getUserList();
    },
    // 获取规格列表
    async getUserList() {
      let res = await getSpecsList()
      this.tableData = res.list;
    },
    // 编辑角色
    edit(info) {
      this.$emit("edit", { ...info });
    },
    // 删除角色弹窗
    del(uid) {
      // 弹窗，二次确认
      this.$confirm("确定删除吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确认按钮
        .then(() => {
          // 真的要删了
          this.delUser(uid);
        })
        // 点击取消按钮
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 真的要删了
    async delUser(id) {
      let res = await userDel(id);
      this.getTotalNum();
    },
    // 切换页码时调用
    handleCurrentChange() {
      this.getUserList();
    },
  },
};
</script>

<style>
</style>